/**
 * 
 */
package com.hfjyz.exam.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.exam.command.LoginCommand;
import com.hfjyz.exam.contants.ExamContants;
import com.hfjyz.exam.model.UserInfo;
import com.hfjyz.exam.service.LoginService;

/**
 *
 */
public class LoginController extends SimpleFormController {
	private LoginService loginService;

	/**
	 * @return the loginService
	 */
	public LoginService getLoginService() {
		return loginService;
	}

	/**
	 * @param loginService
	 *            the loginService to set
	 */
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		UserInfo loginInfo = loginService.login(request,
				(LoginCommand) command, errors);
		if (loginInfo == null || errors.hasErrors()) {
			return new ModelAndView(getFormView(), errors.getModel());
		}
		request.getSession().setAttribute(ExamContants.SESSION_KEY_LOGIN_INFO,
				loginInfo);
		RedirectView rv = new RedirectView("paperSelect.action");
		return new ModelAndView(rv, errors.getModel());
	}
}
